package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfIndexExtractRule;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.mapper.ConfIndexExtractRuleMapper;
import com.yuandian.management.service.ConfIndexExtractRuleService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 指标提取规则-Service实现
 * @author zfsn
 * @date 2023-8-4
 */
@Service
public class ConfIndexExtractRuleServiceImpl extends ServiceImpl<ConfIndexExtractRuleMapper, ConfIndexExtractRule>
        implements ConfIndexExtractRuleService {


	@Override
	public boolean insertIndexExtractRules(List<ConfIndexExtractRule> indexExtractRules) {
		boolean flag = false;
		judgeData(indexExtractRules);

		List<String> resourceCodes = indexExtractRules.stream().map(ConfIndexExtractRule::getResourceCode).distinct().collect(Collectors.toList());
		List<ConfIndexExtractRule> oldDataList = this.list(new LambdaQueryWrapper<ConfIndexExtractRule>().in(ConfIndexExtractRule::getResourceCode, resourceCodes)
				.ne(ConfIndexExtractRule::getIsDel, 1));
		String userName = SecurityUtils.getUser().getName();
		if (CollectionUtils.isEmpty(oldDataList)) {
			for (ConfIndexExtractRule extractRule : indexExtractRules) {
				extractRule.setCreateBy(userName);
				extractRule.setCreateTime(LocalDateTime.now());
				extractRule.setUpdateBy(userName);
				extractRule.setUpdateTime(LocalDateTime.now());
				extractRule.setIsDel(0);
			}
			flag = this.saveBatch(indexExtractRules);
		} else {
			oldDataList.forEach(data -> {
				data.setIsDel(1);
				data.setUpdateBy(userName);
				data.setUpdateTime(LocalDateTime.now());
			});
			this.updateBatchById(oldDataList, oldDataList.size());

			indexExtractRules.forEach(rule -> {
				rule.setCreateBy(userName);
				rule.setCreateTime(LocalDateTime.now());
				rule.setUpdateBy(userName);
				rule.setUpdateTime(LocalDateTime.now());
				rule.setIsDel(0);
			});

			this.saveBatch(indexExtractRules);
		}

		return flag;
	}

	private void judgeData(List<ConfIndexExtractRule> indexExtractRules) {
		Set<String> resourceCodes = indexExtractRules.stream().map(ConfIndexExtractRule::getResourceCode).collect(Collectors.toSet());
		if (resourceCodes.size() == 1 && resourceCodes.contains(null)) {
			throw new RuntimeException("资源编码不能为空");
		}
		if (resourceCodes.size() > 1) {
			throw new RuntimeException("资源编码不能出现多个");
		}

		List<Integer> indexTypeList = indexExtractRules.stream().map(ConfIndexExtractRule::getIndexType).filter(Objects::nonNull).collect(Collectors.toList());
		if (indexExtractRules.size() != indexTypeList.size()) {
			throw new RuntimeException("指标类型不能为空");
		}

		List<String> indexSourceList = indexExtractRules.stream().map(ConfIndexExtractRule::getIndexSource).filter(Objects::nonNull).collect(Collectors.toList());
		if (indexExtractRules.size() != indexSourceList.size()) {
			throw new RuntimeException("指标来源不能为空");
		}
	}

}